<script>
    layui.use(['laypage', 'layer', 'table'], function () {
        var laypage = layui.laypage
            , layer = layui.layer
            , table = layui.table;
        var delBatchUrl = "{{isset($delBatchUrl)?$delBatchUrl:''}}";
        laypage.render({
            elem: 'paginate-render'
            , limit: {{ isset($limit)?$limit:config('administrator.paginate.limit') }}
            , limits: [10, 20, 30, 50, 100, 300, 500, 1000]
            , count: {{ $count }} //数据总数
            , layout: ['limit', 'prev', 'page', 'next']
            , curr: window.jsUrlHelper.getUrlParam(window.location.href.toString(), 'page')
            , jump: function (obj, first) {
                if (!first) {
                    var nUrl = window.jsUrlHelper.putUrlParam(window.location.href.toString(), 'page', obj.curr);
                    nUrl = window.jsUrlHelper.putUrlParam(nUrl, 'limit', obj.limit);
                    window.location.href = nUrl;
                }
            }
        });


        //删除
        $(".form-delete").click(function () {

            var tUrl = $(this).attr('data-url');

            layer.confirm('确认删除吗？', {
                btn: ['确认', '取消']
            }, function (index) {
                $("#delete-form").attr("action", tUrl).submit();
                console.log(tUrl);
                layer.close(index);
                return false;
            }, function (index) {
                layer.close(index);
                return true;
            });

            return false;
        });

        table.on('tool(table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    var ids = [data.id];
                    del(delBatchUrl, ids);
                });
            }
        });

        //批量删除
        var active = {
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('DeleteTable')
                    , data = checkStatus.data;
                var ids = [];
                for (var index in data) {
                    ids[index] = (Number)(data[index].id);
                }
                if (checkStatus.data.length >= 1) {
                    layer.confirm('确定要删除么', function () {
                        del(delBatchUrl, ids);
                    });

                } else {
                    layer.msg('未选中');
                }
            }

        };

        var del = function (delUrl, data) {
            $.ajax({
                url: delUrl,
                data: JSON.stringify(data),
                type: 'delete',
                dataType: 'json',
                success: function (res) {
                    layer.alert(res.message, {icon: 1, time: 2000});
                    window.location.reload();
                },
                error: function (res) {
                    console.log(res);
                    layer.alert(res.message, {icon: 4, time: 3000});

                }
            });
        };


        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


</script>